package com.jeasonzhao.commons.i18n;

import java.util.Hashtable;
import com.jeasonzhao.commons.xml.XMLNode;
import com.jeasonzhao.commons.xml.XMLHelper;
import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.utils.ResourceException;
import com.jeasonzhao.commons.xml.XMLException;

public class I18N
{
    private static final String DEFAULTSECTION = "DEFAULT_8F5D266959E67ED53AED304589A6C138D6A5DCD38361F3D603";
    private boolean m_isCaseSenstive = false;
    private boolean m_isInitialized = false;
    private Hashtable<String,Hashtable<String,String>> sections = null;
    public static final String FILENAME = "locale";
    public static final String DefaultLocale = "zh_CN";
    private I18N()
    {
        super();
    }

    public void setLocale(String str)
    {
        this.m_isInitialized = false;
    }

    public XMLNode findXmlConfig(Class<?> cls,String fileName,String locale)
        throws ResourceException,XMLException
    {
        fileName = Algorithms.oneOfNotNull(fileName,FILENAME);
        locale = Algorithms.oneOfNotNull(locale,DefaultLocale);
        XMLNode node = XMLHelper.fromResource(cls,fileName);
        if(null == node)
        {
            node = XMLHelper.fromResource(cls,fileName + "." + locale);
        }
        if(null == node)
        {
            node = XMLHelper.fromResource(cls,fileName + ".xml");
        }
        if(null == node)
        {
            node = XMLHelper.fromResource(cls,fileName + "." + locale + ".xml");
        }
        return node;
    }

    private String align(String s)
    {
        if(null == s)
        {
            return null;
        }
        else if(this.m_isCaseSenstive == false)
        {
            return s.trim().toLowerCase();
        }
        else
        {
            return s.trim();
        }
    }

    public String get(String key)
    {
        return get(DEFAULTSECTION,key);
    }

    public String get(Class<?> section,String key)
    {
        return get(null == section ? DEFAULTSECTION : section.getName(),key);
    }

    private void init()
    {
        if(m_isInitialized == false)
        {
            synchronized(this)
            {
                if(m_isInitialized)
                {
                    return;
                }
                m_isInitialized = true;
            }
        }
    }
    
    public String get(String section,String key)
    {
        init();
        if(null == sections || key == null || key.trim().length() < 1)
        {
            return null;
        }
        key = align(key);
        section = null == section || section.trim().length() < 1 ? DEFAULTSECTION : align(section);
        if(sections.containsKey(section) == false)
        {
            return "Unknown message section: " + section + " (" + key + ")";
        }
        else
        {
            Hashtable<String,String> hash = sections.get(section);
            if(hash.containsKey(key))
            {
                return hash.get(key);
            }
            else
            {
                return "Unknown message key: " + section + " (" + key + ")";
            }
        }
    }

}
